/**
 * @author xiaoying
 * @time  2022-09-15
 * @title  表单验证规则
 * @desc
 */

export const validates = {
	//密码
	regPwd: /^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?!([^(0-9a-zA-Z)])+$)(?!.*\s)(?!^[\u4e00-\u9fa5]+$).{6,}$/,
	//手机
	regPhone: /^[1][3,4,5,6,7,8][0-9]{9}$/,
	//邮箱
	regEmail: /^[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/,
	//邮箱验证码
	regEmailCode: /^\d{6}$/,
	//正整数
	regPositiveInteger: /^[1-9]\d*$/,
	//一位小数
	regFloat: /^\+?(\d*\.\d{1})$/,
};

//手机号码校验
export const validatePhone = function () {
	return [
		{
			required: true,
			validator: (rule: any, value: string, callback: any) => {
				if (!value) {
					return callback(new Error("请输入账号"));
				}
				if (value && !validates.regPhone.test(value)) {
					callback(new Error("手机号码格式错误！"));
				} else {
					callback();
				}
			},
			trigger: "blur"
		}
	]
}

//密码校验
export const validatePassword = function () {
	return [
		{
			required: true,
			validator: (rule: any, value: string, callback: any) => {
				if (!value) {
					return callback(new Error("请输入密码"));
				}
				if (value && !validates.regPwd.test(value)) {
					callback(new Error('密码至少6位,包含字母及数字或特殊符号(不含空格)！'));
				} else {
					callback();
				}
			},
			trigger: "blur"
		}, {
			min: 6,
			max: 11,
			message: '密码在6~11位之间',
			trigger: 'blur'
		}
	]
}

//邮箱校验必填时
export const validateEmail = function () {
	return [
		{
			required: true,
			validator: (rule: any, value: string, callback: any) => {
				if (value && !validates.regEmail.test(value)) {
					callback('邮箱格式错误！');
				}
				callback();
			},
			trigger: "blur"
		}
	]
}
// 邮箱非必填时的验证
// export const validateEmail = function (rule: any, value: string, callback: any) {
// 	if (value && !validates.regEmail.test(value)) {
// 		callback('邮箱格式错误！');
// 	}
// 	callback();
// }
// 使用方法：
// email: [{  validator: validateEmail, trigger: "blur" }],

//长度不为空验证
export function fromLength(message: string) {
	return [{
		required: true,
		message: message,
		trigger: 'blur'
	},
	{
		min: 1,
		message: '长度在1个字符以上',
		trigger: 'blur'
	}
	];
}
// 必填校验
export function fromRequired(message: string) {
	return [
		{ required: true, message: message, trigger: 'blur' }
	];
}